package cc.rengu.redp.bizimpl.upmp.service.impl;


/*import cc.rengu.ecp.core.exception.ServiceException;
import cc.rengu.ecp.core.util.Common;
import cc.rengu.ecp.platform.mcmp.util.CommonUtils;
import cc.rengu.ecp.platform.module.auth.entity.User;
import cc.rengu.ecp.platform.module.sys.entity.DataDict;
import cc.rengu.ecp.platform.module.sys.service.DataDictService;
import cc.rengu.ecp.core.web.view.OperationResult;
import cc.rengu.ecp.platform.core.security.AuthContextHolder;
import cc.rengu.ecp.platform.mcmp.bean.*;
import cc.rengu.ecp.platform.mcmp.enums.BompTxnNumEnum;
import cc.rengu.ecp.platform.mcmp.enums.IgasAcctStatusEnum;*/

import cc.rengu.igas.bomp.facade.BompManualAdjustAcctService;
import cc.rengu.igas.bomp.facade.request.MompPaymentRequest;
import cc.rengu.igas.bomp.facade.response.MompPaymentResponse;
import cc.rengu.igas.momp.facade.*;
import cc.rengu.igas.momp.facade.base.Header;
import cc.rengu.igas.momp.facade.bean.*;
import cc.rengu.igas.momp.facade.request.*;
import cc.rengu.igas.momp.facade.response.*;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.redp.bizimpl.enums.BompTxnNumEnum;
import cc.rengu.redp.bizimpl.enums.IgasAcctStatusEnum;
import cc.rengu.redp.bizimpl.enums.MompTxnNumEnum;
import cc.rengu.redp.bizimpl.upmp.bean.CustomerPublishRequest;
import cc.rengu.redp.bizimpl.upmp.bean.MarketManagerRequest;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.service.ICustomerMarketService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntBaseInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.*;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

@DS("upmp")
@Service
public class CustomerMarketServiceImpl implements ICustomerMarketService {

    public static Logger log = LoggerFactory.getLogger(CustomerMarketServiceImpl.class);

//  @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62145", check = false)
  @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private QueryMcListInfoService queryMcListInfoService;

//  @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62145", check = false)
    @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private CouponManagementService couponManagementService;

//    @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62145", check = false)
    @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private EditMcDetailService editMcDetailService;

//    @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62145", check = false)
    @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private QueryMcDetailInfoService detailInfoService;

//    @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62145", check = false)
    @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private ManageMcService manageMcService;

//    @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62145", check = false)
    @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private PublishMcService publishMcService;

//    @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62145", check = false)
    @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private QueryStatisticsService queryStatisticsService;

//    @DubboReference(version = "2.2.0", url = "dubbo://172.17.100.9:62132", check = false)
    @DubboReference(version = "${dubbo.protocol.momp.version}", check = false)
    private BompManualAdjustAcctService bompManualAdjustAcctService;

    @Autowired
    private IUpmpBMchntBaseInfoService bMchntBaseInfoService;

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

/**
 * 卡券活动明细列表查询
 *
 * @param reqDto
 * @param pageable
 * @return
 */
    public IPage<CouponListInfoBean> findCouponDetailByPage(QueryCouponDetailRequest reqDto, QueryRequest pageable) throws RedpException {
        Result<QueryCouponDetailResponse> res = new Result<>();
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        reqDto.setPageIndex(String.valueOf(pageable.getPageNum()));
        reqDto.setPageSize(String.valueOf(pageable.getPageSize()));

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.COUPON_DETAIL_QUERY.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.COUPON_DETAIL_QUERY.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        try {
            log.info("卡券活动明细列表查询 请求报文：{}", JSON.toJSONString(reqDto));
            res = couponManagementService.queryCouponDetail(reqDto);
            log.info("卡券活动明细列表查询 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("卡券活动明细列表查询时系统异常:[{}]", e);
            throw new RedpException("卡券活动概览列表查询时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("卡券活动明细列表查询时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("卡券活动明细列表查询时接口调用失败");
        }
        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("卡券活动明细列表查询时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("卡券活动明细列表查询时失败:" + res.getResult().getRespDesc());
        }
        if (null == res.getResult().getCouponInfoList()) {
            log.info("卡券活动明细列表查询时没有返回数据:result=[{}]", JSON.toJSONString(res.getResult()));
            return null;
        }
        return null;
    }

/**
 * 减免类营销活动的编辑
 *
 * @param entity
 */

    public void editMoneyOffMcSave(CustomerMcInfoVO entity) throws RedpException {
        //转换参数
        EditMoneyOffMcDetailRequest reqDto = getEditMoneyOffMcBean(entity);
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        reqDto.setMrctFilePath(entity.getMrctFilePath());

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.MONEYOFF_MC_EDIT.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.MONEYOFF_MC_EDIT.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);
        Result<EditMoneyOffMcDetailResponse> res = new Result<>();
        try {
            log.info("减免类营销活动编辑 请求报文：{}", JSON.toJSONString(reqDto));
            res = editMcDetailService.editMoneyOffMcDetail(reqDto);
            log.info("减免类营销活动编辑 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("减免类营销活动编辑时系统异常:[{}]", e);
            throw new RedpException("对客营销活动编辑时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("减免类营销活动编辑时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("减免类营销活动编辑时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("减免类营销活动编辑时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("减免类营销活动编辑时失败:" + res.getResult().getRespDesc());
        }
    }

/**
 * 减免类编辑时的转换
 *
 * @param entity
 * @return
 */
    private EditMoneyOffMcDetailRequest getEditMoneyOffMcBean(CustomerMcInfoVO entity) {
        EditMoneyOffMcDetailRequest bean = new EditMoneyOffMcDetailRequest();
        bean.setOprType(entity.getOprType());
        bean.setQueryType(entity.getQueryType());
        bean.setQueryRole(entity.getQueryRole());
        MoneyOffMcInfoBean mcInfoObj = new MoneyOffMcInfoBean();
        mcInfoObj.setMcNo(entity.getMcNo());
        mcInfoObj.setMcName(entity.getMcName());
        mcInfoObj.setMcType(entity.getMcType());
        mcInfoObj.setMcStatus(entity.getMcStatus());
        Date start = DateUtils.parseDate(entity.getMcStartDate(), DateUtils.DEFAULT_DATE_FORMAT);
        Date end = DateUtils.parseDate(entity.getMcEndDate(), DateUtils.DEFAULT_DATE_FORMAT);
        mcInfoObj.setMcStartDate(DateUtils.formatDate(start, DateUtils.FORMAT_YYYYMMDD));
        mcInfoObj.setMcEndDate(DateUtils.formatDate(end, DateUtils.FORMAT_YYYYMMDD));
        String sponsor = entity.getSponsor();
        if ("0".equals(sponsor)) {
            mcInfoObj.setBankBudget("0");
            mcInfoObj.setExpectMchntQty("0");
        } else if ("1".equals(sponsor)) {
            mcInfoObj.setBankBudget(entity.getBankBudget());
            mcInfoObj.setExpectMchntQty(entity.getExpectMchntQty());
        } else {
            log.info("CustomerMarketService:getEditMoneyOffMcBean");
        }
        mcInfoObj.setSponsor(sponsor);
        String mcTimeFlag = entity.getMcTimeFlag();
        mcInfoObj.setMcTimeFlag(mcTimeFlag);
        if ("1".equals(mcTimeFlag)) {
            //设置时间信息
            String[] week = entity.getWeek();
            List weekList = Arrays.asList(week);
            String timeJson = StringEscapeUtils.unescapeHtml4(entity.getTimeList());
            List<McTimeInfoBean> timeInfoList = JSONArray.parseArray(timeJson, McTimeInfoBean.class);
            int j = 1;
            for (McTimeInfoBean timeBean : timeInfoList) {
                timeBean.setRuleIndex(String.valueOf(j));
                if (weekList.contains("1")) {
                    timeBean.setFlagMonday("1");
                } else {
                    timeBean.setFlagMonday("0");
                }
                if (weekList.contains("2")) {
                    timeBean.setFlagTuesday("1");
                } else {
                    timeBean.setFlagTuesday("0");
                }
                if (weekList.contains("3")) {
                    timeBean.setFlagWednesday("1");
                } else {
                    timeBean.setFlagWednesday("0");
                }
                if (weekList.contains("4")) {
                    timeBean.setFlagThursday("1");
                } else {
                    timeBean.setFlagThursday("0");
                }
                if (weekList.contains("5")) {
                    timeBean.setFlagFriday("1");
                } else {
                    timeBean.setFlagFriday("0");
                }
                if (weekList.contains("6")) {
                    timeBean.setFlagSaturday("1");
                } else {
                    timeBean.setFlagSaturday("0");
                }
                if (weekList.contains("7")) {
                    timeBean.setFlagSunday("1");
                } else {
                    timeBean.setFlagSunday("0");
                }
                j++;
            }
            bean.setTimeInfoList(timeInfoList);
        }
        mcInfoObj.setRestriction(entity.getRestriction());
        mcInfoObj.setOdcrdDisTimes(entity.getOdcrdDisTimes());
        mcInfoObj.setPrkBusnFlg(entity.getPrkBusnFlg());
        mcInfoObj.setMcLvlCd(entity.getMcLvlCd());
        String clientRange = entity.getClientRange();
        mcInfoObj.setClientRange(clientRange);
        if ("1".equals(clientRange)) {
            //设置用户信息
            List<McUserInfoBean> userInfoList = new ArrayList<>();
            if (StringUtils.isNotBlank(entity.getPhoneList())) {
                String phoneJson = StringEscapeUtils.unescapeHtml4(entity.getPhoneList());
                List<McUserInfoBean> phoneList = JSONArray.parseArray(phoneJson, McUserInfoBean.class);
                userInfoList.addAll(phoneList);
            }
            if (StringUtils.isNotBlank(entity.getCardList())) {
                String cardJson = StringEscapeUtils.unescapeHtml4(entity.getCardList());
                List<McUserInfoBean> cardList = JSONArray.parseArray(cardJson, McUserInfoBean.class);
                userInfoList.addAll(cardList);
            }
            if (StringUtils.isNotBlank(entity.getKbinList())) {
                String kbinJson = StringEscapeUtils.unescapeHtml4(entity.getKbinList());
                List<McUserInfoBean> kbinList = JSONArray.parseArray(kbinJson, McUserInfoBean.class);
                userInfoList.addAll(kbinList);
            }
            if (StringUtils.isNotBlank(entity.getKbinProdList())) {
                String kbinProdJson = StringEscapeUtils.unescapeHtml4(entity.getKbinProdList());
                List<McUserInfoBean> kbinProdList = JSONArray.parseArray(kbinProdJson, McUserInfoBean.class);
                userInfoList.addAll(kbinProdList);
            }
            bean.setUserInfoList(userInfoList);
        }
        String merchantRange = entity.getMerchantRange();
        mcInfoObj.setMerchantRange(merchantRange);
        if ("0".equals(merchantRange)) {
            //商户信息
            List<McMchntInfoBean> mcMchntList = new ArrayList<>();
            mcMchntList.addAll(entity.getMerchantList());
            mcMchntList.addAll(entity.getCircleList());
            bean.setMchntInfoList(mcMchntList);
        } else if ("1".equals(merchantRange)) {
            mcInfoObj.setNewMchntFlag(entity.getNewMchntFlagDeposit());
            mcInfoObj.setAmtLimitFloor(entity.getAmtLimitFloorDeposit());
            mcInfoObj.setAmtLimitCelling(entity.getAmtLimitCellingDeposit());
        } else if ("2".equals(merchantRange)) {
            mcInfoObj.setNewMchntFlag(entity.getNewMchntFlagMoney());
            mcInfoObj.setAmtLimitFloor(entity.getAmtLimitFloorMoney());
            mcInfoObj.setAmtLimitCelling(entity.getAmtLimitCellingMoney());
        } else {
            log.info("CustomerMarketService:getEditMoneyOffMcBean");
        }
        mcInfoObj.setBudget(entity.getBudget());
        if("3".equals(entity.getSponsor())){
            mcInfoObj.setBudgetRechargeMethod("0");// 活动后结算
        }else{
            mcInfoObj.setBudgetRechargeMethod("1");// 预算充值方式
        }
        mcInfoObj.setAuditStatus(entity.getAuditStatus());
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        mcInfoObj.setInstId(user.getInstId());//机构编码
        if (StringUtils.isBlank(entity.getMcNo())) {
            //为空表示新增,不为空表示修改
            mcInfoObj.setOprId(user.getId());//创建人
            mcInfoObj.setCreateTime(DateUtils.formatDate(new Date(), DateUtils.DEFAULT_TIME_FORMAT));
        } else {
            mcInfoObj.setOprId(entity.getOprId());//创建人
            mcInfoObj.setCreateTime(entity.getCreateTime());
            mcInfoObj.setLastOprId(user.getId());
            mcInfoObj.setUpdateTime(DateUtils.formatDate(new Date(), DateUtils.DEFAULT_TIME_FORMAT));
        }

        bean.setMcInfoObj(mcInfoObj);

        //机构信息
        String orgInfoJson = StringEscapeUtils.unescapeHtml4(entity.getOrgInfoList());
        List<McOrgInfoBean> orgInfoList = JSONArray.parseArray(orgInfoJson, McOrgInfoBean.class);
        bean.setOrgInfoList(orgInfoList);
        //规则信息
        String ruleInfoJson = StringEscapeUtils.unescapeHtml4(entity.getRuleInfoList());
        List<MoneyOffRuleInfoBean> ruleInfoList = JSONArray.parseArray(ruleInfoJson, MoneyOffRuleInfoBean.class);
        int i = 1;
        for (MoneyOffRuleInfoBean ruleInfoBean : ruleInfoList) {
            ruleInfoBean.setRuleIndex(String.valueOf(i));
            i++;
        }
        bean.setRuleInfoList(ruleInfoList);
        //产品信息
        List<McProductInfoBean> productInfoList = new ArrayList<>();
        List<String> upLineProduce = entity.getUpLineProduce();
        List<String> onLineProduce = entity.getOnLineProduce();
        if (null != upLineProduce && upLineProduce.size() > 0) {
            for (String code : upLineProduce) {
                McProductInfoBean infoBean = new McProductInfoBean();
                infoBean.setBizProdCode(code);
                productInfoList.add(infoBean);
            }
        }
        if (null != onLineProduce && onLineProduce.size() > 0) {
            for (String code : onLineProduce) {
                McProductInfoBean infoBean = new McProductInfoBean();
                infoBean.setBizProdCode(code);
                productInfoList.add(infoBean);
            }
        }
        bean.setProductInfoList(productInfoList);
        return bean;
    }

/**
 * 卡券类营销活动的编辑
 *
 * @param entity
 */
    public void editCouponMcSave(CustomerMcInfoVO entity) throws RedpException {
        //转换参数
        EditCouponMcDetailRequest reqDto = getEditCouponMcBean(entity);
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        reqDto.setMrctFilePath(entity.getMrctFilePath());
        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.COUPON_MC_EDIT.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.COUPON_MC_EDIT.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);
        Result<EditCouponMcDetailResponse> res = new Result<>();
        try {
            log.info("卡券类营销活动编辑 请求报文：{}", JSON.toJSONString(reqDto));
            res = editMcDetailService.editCouponMcDetail(reqDto);
            log.info("卡券类营销活动编辑 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("卡券类营销活动编辑时系统异常:[{}]", e);
            throw new RedpException("对客营销活动编辑时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("卡券类营销活动编辑时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("卡券类营销活动编辑时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("卡券类营销活动编辑时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("卡券类营销活动编辑时失败:" + res.getResult().getRespDesc());
        }
    }

/**
 * 卡券类编辑时的转换
 *
 * @param entity
 * @return
 */
    private EditCouponMcDetailRequest getEditCouponMcBean(CustomerMcInfoVO entity) {
        EditCouponMcDetailRequest bean = new EditCouponMcDetailRequest();
        bean.setOprType(entity.getOprType());
        bean.setQueryType(entity.getQueryType());
        bean.setQueryRole(entity.getQueryRole());
        CouponMcInfoBean mcInfoObj = new CouponMcInfoBean();
        mcInfoObj.setMcNo(entity.getMcNo());
        mcInfoObj.setMcName(entity.getMcName());
        mcInfoObj.setClaimChannelId(entity.getClaimChannelId());
        String mcType = entity.getMcType();
        if ("10".equals(mcType)) {
            mcInfoObj.setPurchaseFlag("0");
        } else if ("11".equals(mcType)) {
            mcInfoObj.setPurchaseFlag("1");
        } else {
            log.info("CustomerMarketService:getEditCouponMcBean");
        }
        mcInfoObj.setMcType(entity.getMcType());
        mcInfoObj.setMcStatus(entity.getMcStatus());
        Date start = DateUtils.parseDate(entity.getMcStartDate(), DateUtils.DEFAULT_DATE_FORMAT);
        Date end = DateUtils.parseDate(entity.getMcEndDate(), DateUtils.DEFAULT_DATE_FORMAT);
        mcInfoObj.setMcStartDate(DateUtils.formatDate(start, DateUtils.FORMAT_YYYYMMDD));
        mcInfoObj.setMcEndDate(DateUtils.formatDate(end, DateUtils.FORMAT_YYYYMMDD));
        String sponsor = entity.getSponsor();
        if ("0".equals(sponsor)) {
            mcInfoObj.setBankBudget("0");
            mcInfoObj.setExpectMchntQty("0");
        } else if ("1".equals(sponsor)) {
            mcInfoObj.setBankBudget(entity.getBankBudget());
            mcInfoObj.setExpectMchntQty(entity.getExpectMchntQty());
        } else {
            log.info("CustomerMarketService:getEditCouponMcBean");
        }
        mcInfoObj.setSponsor(sponsor);
        String mcTimeFlag = entity.getMcTimeFlag();
        mcInfoObj.setMcTimeFlag(mcTimeFlag);
        if ("1".equals(mcTimeFlag)) {
            //设置时间信息
            String[] week = entity.getWeek();
            List weekList = Arrays.asList(week);
            String timeJson = StringEscapeUtils.unescapeHtml4(entity.getTimeList());
            List<McTimeInfoBean> timeInfoList = JSONArray.parseArray(timeJson, McTimeInfoBean.class);
            int j = 1;
            for (McTimeInfoBean timeBean : timeInfoList) {
                timeBean.setRuleIndex(String.valueOf(j));
                if (weekList.contains("1")) {
                    timeBean.setFlagMonday("1");
                } else {
                    timeBean.setFlagMonday("0");
                }
                if (weekList.contains("2")) {
                    timeBean.setFlagTuesday("1");
                } else {
                    timeBean.setFlagTuesday("0");
                }
                if (weekList.contains("3")) {
                    timeBean.setFlagWednesday("1");
                } else {
                    timeBean.setFlagWednesday("0");
                }
                if (weekList.contains("4")) {
                    timeBean.setFlagThursday("1");
                } else {
                    timeBean.setFlagThursday("0");
                }
                if (weekList.contains("5")) {
                    timeBean.setFlagFriday("1");
                } else {
                    timeBean.setFlagFriday("0");
                }
                if (weekList.contains("6")) {
                    timeBean.setFlagSaturday("1");
                } else {
                    timeBean.setFlagSaturday("0");
                }
                if (weekList.contains("7")) {
                    timeBean.setFlagSunday("1");
                } else {
                    timeBean.setFlagSunday("0");
                }
                j++;
            }
            bean.setTimeInfoList(timeInfoList);
        }
        mcInfoObj.setRestriction(entity.getRestriction());
        String clientRange = entity.getClientRange();
        mcInfoObj.setClientRange(clientRange);
        if ("1".equals(clientRange)) {
            //设置用户信息
            List<McUserInfoBean> userInfoList = new ArrayList<>();
            if (StringUtils.isNotBlank(entity.getPhoneList())) {
                String phoneJson = StringEscapeUtils.unescapeHtml4(entity.getPhoneList());
                List<McUserInfoBean> phoneList = JSONArray.parseArray(phoneJson, McUserInfoBean.class);
                userInfoList.addAll(phoneList);
            }
            if (StringUtils.isNotBlank(entity.getCardList())) {
                String cardJson = StringEscapeUtils.unescapeHtml4(entity.getCardList());
                List<McUserInfoBean> cardList = JSONArray.parseArray(cardJson, McUserInfoBean.class);
                userInfoList.addAll(cardList);
            }
            if (StringUtils.isNotBlank(entity.getKbinList())) {
                String kbinJson = StringEscapeUtils.unescapeHtml4(entity.getKbinList());
                List<McUserInfoBean> kbinList = JSONArray.parseArray(kbinJson, McUserInfoBean.class);
                userInfoList.addAll(kbinList);
            }
            if (StringUtils.isNotBlank(entity.getKbinProdList())) {
                String kbinProdJson = StringEscapeUtils.unescapeHtml4(entity.getKbinProdList());
                List<McUserInfoBean> kbinProdList = JSONArray.parseArray(kbinProdJson, McUserInfoBean.class);
                userInfoList.addAll(kbinProdList);
            }
            bean.setUserInfoList(userInfoList);
        }
        String merchantRange = entity.getMerchantRange();
        mcInfoObj.setMerchantRange(merchantRange);
        if ("0".equals(merchantRange)) {
            //商户信息
            List<McMchntInfoBean> mcMchntList = new ArrayList<>();
            mcMchntList.addAll(entity.getMerchantList());
            mcMchntList.addAll(entity.getCircleList());
            bean.setMchntInfoList(mcMchntList);
        } else if ("1".equals(merchantRange)) {
            mcInfoObj.setNewMchntFlag(entity.getNewMchntFlagDeposit());
            mcInfoObj.setAmtLimitFloor(entity.getAmtLimitFloorDeposit());
            mcInfoObj.setAmtLimitCelling(entity.getAmtLimitCellingDeposit());
        } else if ("2".equals(merchantRange)) {
            mcInfoObj.setNewMchntFlag(entity.getNewMchntFlagMoney());
            mcInfoObj.setAmtLimitFloor(entity.getAmtLimitFloorMoney());
            mcInfoObj.setAmtLimitCelling(entity.getAmtLimitCellingMoney());
        } else {
            log.info("CustomerMarketService:getEditCouponMcBean");
        }
        mcInfoObj.setBudget(entity.getBudget());

        if("3".equals(entity.getSponsor())){
            mcInfoObj.setBudgetRechargeMethod("0");// 活动后结算
        }else{
            mcInfoObj.setBudgetRechargeMethod("1");// 预算充值方式
        }
        mcInfoObj.setAuditStatus(entity.getAuditStatus());
        mcInfoObj.setAvailableDate(entity.getAvailableDate());
        String couponDateFlag = entity.getCouponDateFlag();
        mcInfoObj.setCouponDateFlag(couponDateFlag);
        if ("0".equals(couponDateFlag)) {
            Date cstart = DateUtils.parseDate(entity.getCouponStartDate(), DateUtils.DEFAULT_DATE_FORMAT);
            Date cend = DateUtils.parseDate(entity.getCouponEndDate(), DateUtils.DEFAULT_DATE_FORMAT);
            mcInfoObj.setCouponStartDate(DateUtils.formatDate(cstart, DateUtils.FORMAT_YYYYMMDD));
            mcInfoObj.setCouponEndDate(DateUtils.formatDate(cend, DateUtils.FORMAT_YYYYMMDD));
        } else if ("1".equals(couponDateFlag)) {
            mcInfoObj.setCouponPeriod(entity.getCouponPeriod());
        } else {
            log.info("CustomerMarketService:getEditCouponMcBean");
        }
        mcInfoObj.setCouponInventory(entity.getCouponInventory()); //卡券库存
        mcInfoObj.setCouponClaimed("0");
        mcInfoObj.setCouponRedeemed("0");
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        mcInfoObj.setInstId(user.getInstId());//机构编码
        if (StringUtils.isBlank(entity.getMcNo())) {
            //为空表示新增,不为空表示修改
            mcInfoObj.setOprId(user.getId());//创建人
            mcInfoObj.setCreateTime(DateUtils.formatDate(new Date(), DateUtils.DEFAULT_TIME_FORMAT));
        } else {
            mcInfoObj.setOprId(entity.getOprId());//创建人
            mcInfoObj.setCreateTime(entity.getCreateTime());
            mcInfoObj.setLastOprId(user.getId());
            mcInfoObj.setUpdateTime(DateUtils.formatDate(new Date(), DateUtils.DEFAULT_TIME_FORMAT));
        }
        bean.setMcInfoObj(mcInfoObj);
        //机构信息
        String orgInfoJson = StringEscapeUtils.unescapeHtml4(entity.getOrgInfoList());
        List<McOrgInfoBean> orgInfoList = JSONArray.parseArray(orgInfoJson, McOrgInfoBean.class);
        bean.setOrgInfoList(orgInfoList);
        //规则信息
        String ruleInfoJson = StringEscapeUtils.unescapeHtml4(entity.getRuleInfoList());
        List<CouponRuleInfoBean> ruleInfoList = JSONArray.parseArray(ruleInfoJson, CouponRuleInfoBean.class);
        int i = 1;
        for (CouponRuleInfoBean ruleInfoBean : ruleInfoList) {
            ruleInfoBean.setRuleIndex(String.valueOf(i));
            i++;
        }
        bean.setRuleInfoList(ruleInfoList);
        //产品信息
        List<McProductInfoBean> productInfoList = new ArrayList<>();
        List<String> upLineProduce = entity.getUpLineProduce();
        List<String> onLineProduce = entity.getOnLineProduce();
        if (null != upLineProduce && upLineProduce.size() > 0) {
            for (String code : upLineProduce) {
                McProductInfoBean infoBean = new McProductInfoBean();
                infoBean.setBizProdCode(code);
                productInfoList.add(infoBean);
            }
        }
        if (null != onLineProduce && onLineProduce.size() > 0) {
            for (String code : onLineProduce) {
                McProductInfoBean infoBean = new McProductInfoBean();
                infoBean.setBizProdCode(code);
                productInfoList.add(infoBean);
            }
        }
        bean.setProductInfoList(productInfoList);
        return bean;
    }

/**
 * 根据活动编号查询减免类活动详情
 *
 * @param mcNo
 * @return
 */
    public QueryMoneyOffMcDetailInfoResponse getMoneyOffMcDetailData(String mcNo, String queryType, String queryRole) throws RedpException {
        QueryMoneyOffMcDetailInfoRequest reqDto = new QueryMoneyOffMcDetailInfoRequest();
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());
        reqDto.setMcNo(mcNo);
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        reqDto.setQueryType(queryType);
        reqDto.setQueryRole(queryRole);

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.MONEYOFF_MC_DETAIL.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.MONEYOFF_MC_DETAIL.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        Result<QueryMoneyOffMcDetailInfoResponse> res = new Result<>();
        try {
            log.info("查询减免类营销活动详情 请求报文：{}", JSON.toJSONString(reqDto));
            res = detailInfoService.queryMoneyOffMcDetailInfo(reqDto);
            log.info("查询减免类营销活动详情 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("查询减免类营销活动详情时系统异常:[{}]", e);
            throw new RedpException("查询营销活动详情时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("查询减免类营销活动详情时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("查询减免类营销活动详情时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("查询减免类营销活动详情时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("查询减免类营销活动详情时失败:" + res.getResult().getRespDesc());
        }

        return res.getResult();
    }

/**
 * 根据活动编号查询卡券类活动详情
 *
 * @param mcNo
 * @return
 */
    public QueryCouponMcDetailInfoResponse getCouponMcDetailData(String mcNo, String queryType, String queryRole) throws RedpException {
        QueryCouponMcDetailInfoRequest reqDto = new QueryCouponMcDetailInfoRequest();
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());
        reqDto.setMcNo(mcNo);
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        reqDto.setQueryType(queryType);
        reqDto.setQueryRole(queryRole);

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.COUPON_MC_DETAIL.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.COUPON_MC_DETAIL.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        Result<QueryCouponMcDetailInfoResponse> res = new Result<>();
        try {
            log.info("查询卡券类营销活动详情 请求报文：{}", JSON.toJSONString(reqDto));
            res = detailInfoService.queryCouponMcDetailInfo(reqDto);
            log.info("查询卡券类营销活动详情 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("查询卡券类营销活动详情时系统异常:[{}]", e);
            throw new RedpException("查询营销活动详情时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("查询卡券类营销活动详情时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("查询卡券类营销活动详情时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("查询卡券类营销活动详情时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("查询卡券类营销活动详情时失败:" + res.getResult().getRespDesc());
        }

        return res.getResult();
    }


/*
*
 * 减免类营销活动的发布
 *
 * @param entity

*/
    public OperationResult publishMoneyOffMc(Map<String, Object> map, CustomerPublishRequest entity, String msg, boolean isMompPayment) throws RedpException {
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        PublishMoneyOffMcRequest reqDto = getPublishMoneyOffMcBean(entity);
        String skipPub = "";
        if (isMompPayment) {
            UpmpSysParam dict=upmpSysParamService.findByPrimaryKey("skipPub",user.getInstId(),null);
            if(Common.isNotBlank(dict)){
                skipPub = dict.getPrimaryValue();
            }
            if(!"1".equals(skipPub)){
                // 调用营销打款接口
                QueryMoneyOffMcDetailInfoResponse respBean = this.setMoneyOffMcModel(map, entity.getMcNo(), entity.getQueryType(), entity.getQueryRole());
                String adjustAmt;
                // 打款金额
                if ("0".equals(respBean.getMcInfoObj().getSponsor())) {
                    // 银行全资,直接使用营销活动总预算字段即可
                    adjustAmt = respBean.getMcInfoObj().getBudget();
                } else if("1".equals(respBean.getMcInfoObj().getSponsor())){
                    // 联合营销活动,直接使用银行出资预算字段即可
                    adjustAmt = respBean.getMcInfoObj().getBankBudget();
                }else{
                    adjustAmt="0";//商户免充值(线下结算)，银行不需要打钱
                }
                //只有金额大于0时 才调用打款接口
                if(StringUtils.isNotEmpty(adjustAmt)&&Float.parseFloat(adjustAmt)>0){
                    OperationResult result = mompPayment(entity.getMcNo(), reqDto.getAcctInfoObk(), user, adjustAmt);
                    if (!OperationResult.SUCCESS.equals(result.getCode())) {
                        log.error("调用营销打款接口失败");
                        return OperationResult.buildFailureResult("调用营销打款接口失败，请联系管理员");
                    }
                }
            }
        }
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.MONEYOFF_MC_PUBLISH.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + user.getOrgCode() + MompTxnNumEnum.MONEYOFF_MC_PUBLISH.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        Result<PublishMoneyOffMcResponse> res = new Result<>();
        try {
            log.info("减免类营销活动" + msg + " 请求报文：{}", JSON.toJSONString(reqDto));
            res = publishMcService.publishMoneyOffMc(reqDto);
            log.info("减免类营销活动" + msg + " 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("减免类营销活动" + msg + "时系统异常:[{}]", e);
            return OperationResult.buildFailureResult("对客营销活动" + msg + "时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("减免类营销活动" + msg + "时接口调用失败:result=[{}]", JSON.toJSONString(res));
            return OperationResult.buildFailureResult("减免类营销活动" + msg + "时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("减免类营销活动" + msg + "时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            return OperationResult.buildFailureResult("减免类营销活动" + msg + "时失败:" + res.getResult().getRespDesc());
        }

        return OperationResult.buildSuccessResult("发布员" + msg + "成功");
    }

    /**
     * 转换减免类营销活动发布时的请求对象
     *
     * @param entity
     * @return
     */
    private PublishMoneyOffMcRequest getPublishMoneyOffMcBean(CustomerPublishRequest entity) {
        PublishMoneyOffMcRequest bean = new PublishMoneyOffMcRequest();
        bean.setMcNo(entity.getMcNo());
        bean.setOprType(entity.getOprType());
        bean.setOprContent(entity.getOprContent());
        bean.setQueryType(entity.getQueryType());
        bean.setQueryRole(entity.getQueryRole());
        bean.setAcctInfoObk(entity);
        return bean;
    }

/**
 * 卡券类营销活动/发布/暂停/终止/启用
 *
 * @param entity
 *//*

    public OperationResult publishCouponMc(Model model, CustomerPublishRequest entity, String msg, boolean isMompPayment) {
        User user = AuthContextHolder.findAuthUser();
        PublishCouponMcRequest reqDto = getPublishCouponMcBean(entity);
        String skipPub = "";
        if(isMompPayment){
            DataDict dict=dataDictService.findByPrimaryKeyAndInstId("skipPub",user.getInstId());
            if(Common.isNotBlank(dict)){
                skipPub = dict.getPrimaryValue();
            }
            if(!"1".equals(skipPub)){
                // 调用营销打款接口
                QueryCouponMcDetailInfoResponse respBean = this.setCouponMcModel(model, entity.getMcNo(), entity.getQueryType(), entity.getQueryRole());
                String adjustAmt;
                // 打款金额
                if ("0".equals(respBean.getMcInfoObj().getSponsor())) {
                    // 银行全资,直接使用营销活动总预算字段即可
                    adjustAmt = respBean.getMcInfoObj().getBudget();
                } else if("1".equals(respBean.getMcInfoObj().getSponsor())){
                    // 联合营销活动,直接使用银行出资预算字段即可
                    adjustAmt = respBean.getMcInfoObj().getBankBudget();
                }else{
                    adjustAmt="0";//商户免充值(线下结算)，银行不需要打钱
                }
                //只有金额大于0时 才调用打款接口
                if(StringUtils.isNotEmpty(adjustAmt)&&Float.parseFloat(adjustAmt)>0){
                    OperationResult result = mompPayment(entity.getMcNo(), reqDto.getAcctInfoObk(), user, adjustAmt);
                    if (!OperationResult.SUCCESS.equals(result.getCode())) {
                        log.error("调用营销打款接口失败");
                        return OperationResult.buildFailureResult("调用营销打款接口失败，请联系管理员");
                    }
                }
            }
        }
        // 调用发布接口
        String orgCode = getOrgCode(user.getOrgCode());
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.COUPON_MC_PUBLISH.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.COUPON_MC_PUBLISH.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        Result<PublishCouponMcResponse> res = new Result<>();
        try {
            log.info("卡券类营销活动" + msg + " 请求报文：{}", JSON.toJSONString(reqDto));
            res = publishMcService.publishCouponMc(reqDto);
            log.info("卡券类营销活动" + msg + " 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("卡券类营销活动" + msg + "时系统异常:[{}]", e);
            return OperationResult.buildFailureResult("对客营销活动" + msg + "时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("卡券类营销活动" + msg + "时接口调用失败:result=[{}]", JSON.toJSONString(res));
            return OperationResult.buildFailureResult("卡券类营销活动" + msg + "时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("卡券类营销活动" + msg + "时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            return OperationResult.buildFailureResult("卡券类营销活动" + msg + "时失败:" + res.getResult().getRespDesc());
        }

        return OperationResult.buildSuccessResult("发布员" + msg + "成功");
    }

    */

/**
 * 转换卡券类营销活动发布时的请求对象
 *
 * @param entity
 * @return
 *//*

    private PublishCouponMcRequest getPublishCouponMcBean(CustomerPublishRequest entity) {
        PublishCouponMcRequest bean = new PublishCouponMcRequest();
        bean.setMcNo(entity.getMcNo());
        bean.setOprType(entity.getOprType());
        bean.setOprContent(entity.getOprContent());
        bean.setQueryType(entity.getQueryType());
        bean.setQueryRole(entity.getQueryRole());
        bean.setAcctInfoObk(entity);
        return bean;
    }

    */
/**
 * 对客类营销活动报表查询
 *
 * @param reqDto
 * @return
 */

    public BankToClientMcStatInfoBean reportActivity(QueryBankToClientMcStatRequest reqDto) throws RedpException {
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.BANKTOCLIENT_MC_STAT.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.BANKTOCLIENT_MC_STAT.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        Result<QueryBankToClientMcStatResponse> res = new Result<>();
        try {
            log.info("银行对客类营销活动报表查询 请求报文：{}", JSON.toJSONString(reqDto));
            res = queryStatisticsService.queryBankToClientMcStat(reqDto);
            log.info("银行对客类营销活动报表查询 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("银行对客类营销活动报表查询时系统异常:[{}]", e);
            throw new RedpException("银行对客类营销活动报表查询时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("银行对客类营销活动报表查询时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("银行对客类营销活动报表查询时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("银行对客类营销活动报表查询时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("银行对客类营销活动报表查询时失败:" + res.getResult().getRespDesc());
        }
        if (null != res.getResult().getMcStatInfoList() &&
                res.getResult().getMcStatInfoList().size() > 0) {
            return res.getResult().getMcStatInfoList().get(0);
        } else {
            return new BankToClientMcStatInfoBean();
        }
    }

/**
 * 减免类
 * @param mcNo
 * @param queryType
 * @param queryRole
 */

    public QueryMoneyOffMcDetailInfoResponse setMoneyOffMcModel(Map<String, Object> map, String mcNo, String queryType, String queryRole) throws RedpException {
        CustomerMcInfoVO entity = new CustomerMcInfoVO();
        QueryMoneyOffMcDetailInfoResponse respBean = this.getMoneyOffMcDetailData(mcNo, queryType, queryRole);
        MoneyOffMcInfoBean mcInfoBean = respBean.getMcInfoObj();
        entity.setNfrcTmnlRsn(mcInfoBean.getNfrcTmnlRsn());
        entity.setMcNo(mcInfoBean.getMcNo());
        entity.setMcName(mcInfoBean.getMcName());
        entity.setAuditRefuseReason(mcInfoBean.getAuditRefuseReason());
        entity.setPublishRefuseReason(mcInfoBean.getPublishRefuseReason());
        String sponsor = mcInfoBean.getSponsor();
        if ("0".equals(sponsor)) {
            map.put("sponsor", "银行全资");
        } else if ("1".equals(sponsor)) {
            map.put("sponsor", "银行商户联合");
        } else if ("2".equals(sponsor)){
            map.put("sponsor", "商户全资");
        }else{
            map.put("sponsor", "商户全资免充值");
        }
        entity.setSponsor(sponsor);
        String budgetRechargeMethod=mcInfoBean.getBudgetRechargeMethod();
        if("0".equals(budgetRechargeMethod)){
            map.put("budgetRechargeMethod", "活动后结算");
        }else{
            map.put("budgetRechargeMethod", "预先打款");
        }
        entity.setBankBudget(mcInfoBean.getBankBudget());
        entity.setExpectMchntQty(mcInfoBean.getExpectMchntQty());
        entity.setBudget(mcInfoBean.getBudget());
        String mcType = mcInfoBean.getMcType();
        if ("00".equals(mcType)) {
            map.put("mcType", "定额立减");
            entity.setMcMethod("0");
        } else if ("01".equals(mcType)) {
            map.put("mcType", "随机立减");
            entity.setMcMethod("0");
        } else if ("02".equals(mcType)) {
            map.put("mcType", "固定折扣立减");
            entity.setMcMethod("0");
        } else if ("03".equals(mcType)) {
            map.put("mcType", "随机折扣立减");
            entity.setMcMethod("0");
        } else if ("10".equals(mcType)) {
            map.put("mcType", "免费型代金券");
            entity.setMcMethod("1");
        } else if ("11".equals(mcType)) {
            map.put("mcType", "购买型代金券");
            entity.setMcMethod("1");
        } else {
            map.put("mcType", mcType);
        }
        entity.setMcType(mcType);
        Date start = DateUtils.parseDate(mcInfoBean.getMcStartDate(), DateUtils.FORMAT_YYYYMMDD);
        Date end = DateUtils.parseDate(mcInfoBean.getMcEndDate(), DateUtils.FORMAT_YYYYMMDD);
        entity.setMcStartDate(DateUtils.formatDate(start, DateUtils.DEFAULT_DATE_FORMAT));
        entity.setMcEndDate(DateUtils.formatDate(end, DateUtils.DEFAULT_DATE_FORMAT));
        entity.setMcTimeFlag(mcInfoBean.getMcTimeFlag());
        entity.setRestriction(mcInfoBean.getRestriction());
        entity.setOdcrdDisTimes(mcInfoBean.getOdcrdDisTimes());
        entity.setPrkBusnFlg(mcInfoBean.getPrkBusnFlg());
        entity.setMcLvlCd(mcInfoBean.getMcLvlCd());
        String clientRange = mcInfoBean.getClientRange();
        if ("0".equals(clientRange)) {
            map.put("clientRange", "所有人");
        } else if ("1".equals(clientRange)) {
            map.put("clientRange", "指定用户");
        } else {
            map.put("clientRange", clientRange);
        }
        entity.setClientRange(clientRange);
        String mcLvlCd = mcInfoBean.getMcLvlCd();
        if ("2".equals(mcLvlCd)) {
            map.put("mcLvlCd", "行级活动");
        } else if ("1".equals(mcLvlCd)) {
            map.put("mcLvlCd", "商户级活动");
        } else {
            map.put("mcLvlCd", mcLvlCd);
        }
        entity.setMerchantRange(mcInfoBean.getMerchantRange());
        if("1".equals(mcInfoBean.getMerchantRange())){
            entity.setNewMchntFlagDeposit(mcInfoBean.getNewMchntFlag());
            entity.setAmtLimitFloorDeposit(mcInfoBean.getAmtLimitFloor());
            entity.setAmtLimitCellingDeposit(mcInfoBean.getAmtLimitCelling());
        }
        if("2".equals(mcInfoBean.getMerchantRange())){
            entity.setNewMchntFlagMoney(mcInfoBean.getNewMchntFlag());
            entity.setAmtLimitFloorMoney(mcInfoBean.getAmtLimitFloor());
            entity.setAmtLimitCellingMoney(mcInfoBean.getAmtLimitCelling());
        }
        entity.setAuditStatus(mcInfoBean.getAuditStatus());
        //创建人、创建时间、活动状态
        entity.setCreateTime(mcInfoBean.getCreateTime());
        entity.setOprId(mcInfoBean.getOprId());
        entity.setMcStatus(mcInfoBean.getMcStatus());
        map.put("entity", entity);

        //参与机构
        List<String> orgList = new ArrayList<>();
        List<McOrgInfoBean> orgInfoList = respBean.getOrgInfoList();
        if(!ObjectUtils.isEmpty(orgInfoList)){
            for (McOrgInfoBean orgBean : orgInfoList) {
                orgList.add(orgBean.getOrgId());
            }
        }
        map.put("orgList", JSON.toJSONString(orgList));
        //指定用户手机号
        List<McUserInfoBean> userInfoPhones = new ArrayList<>();
        //指定用户银行卡卡号
        List<McUserInfoBean> userInfoCards = new ArrayList<>();
        //指定用户卡BIN
        List<McUserInfoBean> userInfoKbins = new ArrayList<>();
        //指定用户卡产品
        List<McUserInfoBean> userInfoKbinProds = new ArrayList<>();

        List<McUserInfoBean> userInfoList = respBean.getUserInfoList();
        if(!ObjectUtils.isEmpty(userInfoList)) {
            for (McUserInfoBean userBean : userInfoList) {
                if ("0".equals(userBean.getAcctType())) {
                    userInfoPhones.add(userBean);
                } else if ("1".equals(userBean.getAcctType())) {
                    userInfoCards.add(userBean);
                } else if ("2".equals(userBean.getAcctType())) {
                    userInfoKbins.add(userBean);
                } else if ("3".equals(userBean.getAcctType())) {
                    userInfoKbinProds.add(userBean);
                } else {
                    log.info("对客营销活动用户类型");
                }
            }
        }
        map.put("userInfoPhones", userInfoPhones);
        map.put("userInfoCards", userInfoCards);
        map.put("userInfoKbins", userInfoKbins);
        map.put("userInfoKbinProds", userInfoKbinProds);
        //指定商户
        List<McMchntInfoBean> merchantList = new ArrayList<>();
        //指定商圈
        List<McMchntInfoBean> circleList = new ArrayList<>();
        List<McMchntInfoBean> mchntInfoList = respBean.getMchntInfoList();
        if(!ObjectUtils.isEmpty(mchntInfoList)) {
            for (McMchntInfoBean infoBean : mchntInfoList) {
                if ("0".equals(infoBean.getMchntType())) {
                    merchantList.add(infoBean);
                } else if ("1".equals(infoBean.getMchntType())) {
                    circleList.add(infoBean);
                } else {
                    log.info("CustomerMarketService:setMoneyOffMcModel");
                }
            }
        }
        map.put("mchntInfoOldList", JSON.toJSONString(mchntInfoList));
        map.put("merchantList", merchantList);
        map.put("circleList", circleList);
        //优惠策略
        //减免类
        List<CustomerRuleInfoVo> ruleInfos = new ArrayList<>();
        List<MoneyOffRuleInfoBean> ruleInfoList = respBean.getRuleInfoList();
        if(!ObjectUtils.isEmpty(ruleInfoList)) {
            for (MoneyOffRuleInfoBean ruleBean : ruleInfoList) {
                CustomerRuleInfoVo customerRuleInfo = new CustomerRuleInfoVo();
                customerRuleInfo.setDiscountFloor(ruleBean.getDiscountFloor());
                customerRuleInfo.setDiscountCelling(ruleBean.getDiscountCelling());
                customerRuleInfo.setDiscountQuantity(ruleBean.getDiscountQuantity());
                customerRuleInfo.setDiscountLimit(ruleBean.getDiscountLimit());
                customerRuleInfo.setBudget(ruleBean.getBudget());
                customerRuleInfo.setBudgetDayLimit(ruleBean.getBudgetDayLimit());
                customerRuleInfo.setCriteria(ruleBean.getCriteria());
                ruleInfos.add(customerRuleInfo);
            }
        }
        map.put("ruleInfos", ruleInfos);
        //有效期内部分有效期的时段
        List<CustomerTimeInfoVO> timeInfos = new ArrayList<>();
        List<McTimeInfoBean> timeInfoList = respBean.getTimeInfoList();
        if(!ObjectUtils.isEmpty(timeInfoList)) {
            for (McTimeInfoBean timeBean : timeInfoList) {
                CustomerTimeInfoVO timeInfo = new CustomerTimeInfoVO();
                timeInfo.setStartTime(timeBean.getStartTime());
                timeInfo.setEndTime(timeBean.getEndTime());
                timeInfos.add(timeInfo);
            }
        }
        List<String> weekList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(timeInfoList)) {
            McTimeInfoBean mcTimeInfoBean = timeInfoList.get(0);
            if ("1".equals(mcTimeInfoBean.getFlagMonday())) {
                weekList.add("1");
            }
            if ("1".equals(mcTimeInfoBean.getFlagTuesday())) {
                weekList.add("2");
            }
            if ("1".equals(mcTimeInfoBean.getFlagWednesday())) {
                weekList.add("3");
            }
            if ("1".equals(mcTimeInfoBean.getFlagThursday())) {
                weekList.add("4");
            }
            if ("1".equals(mcTimeInfoBean.getFlagFriday())) {
                weekList.add("5");
            }
            if ("1".equals(mcTimeInfoBean.getFlagSaturday())) {
                weekList.add("6");
            }
            if ("1".equals(mcTimeInfoBean.getFlagSunday())) {
                weekList.add("7");
            }
        }
        map.put("timeInfos", timeInfos);
        map.put("week", JSON.toJSONString(weekList));

        //线上、线下营销产品
        List<McProductInfoBean> productInfoList = respBean.getProductInfoList();
        List<String> lineProducekList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(productInfoList)) {
            for (McProductInfoBean infoBean : productInfoList) {
                lineProducekList.add(infoBean.getBizProdCode());
            }
        }
        map.put("lineProducekList", JSON.toJSONString(lineProducekList));
        return respBean;
    }


    /**
     * 卡券类
     *
     * @param mcNo
     * @param queryType
     * @param queryRole
     */
    public QueryCouponMcDetailInfoResponse setCouponMcModel(Map<String, Object> map, String mcNo, String queryType, String queryRole) throws RedpException {
        CustomerMcInfoVO entity = new CustomerMcInfoVO();
        QueryCouponMcDetailInfoResponse respBean = this.getCouponMcDetailData(mcNo, queryType, queryRole);
        CouponMcInfoBean mcInfoBean = respBean.getMcInfoObj();
        entity.setMcNo(mcInfoBean.getMcNo());
        entity.setMcName(mcInfoBean.getMcName());
        entity.setAuditRefuseReason(mcInfoBean.getAuditRefuseReason());
        entity.setPublishRefuseReason(mcInfoBean.getPublishRefuseReason());
        entity.setNfrcTmnlRsn(mcInfoBean.getNfrcTmnlRsn());
        String sponsor = mcInfoBean.getSponsor();
        if ("0".equals(sponsor)) {
            map.put("sponsor", "银行全资");
        } else if ("1".equals(sponsor)) {
            map.put("sponsor", "银行商户联合");
        } else if("2".equals(sponsor)){
            map.put("sponsor", "商户全资");
        } else {
            map.put("sponsor", "商户全资免充值");
        }
        String budgetRechargeMethod=mcInfoBean.getBudgetRechargeMethod();
        if("0".equals(budgetRechargeMethod)){
            map.put("budgetRechargeMethod", "活动后结算");
        }else{
            map.put("budgetRechargeMethod", "预先打款");
        }
        entity.setSponsor(sponsor);
        entity.setBankBudget(mcInfoBean.getBankBudget());
        entity.setExpectMchntQty(mcInfoBean.getExpectMchntQty());
        entity.setBudget(mcInfoBean.getBudget());
        String mcType = mcInfoBean.getMcType();
        if ("00".equals(mcType)) {
            map.put("mcType", "定额立减");
            entity.setMcMethod("0");
        } else if ("01".equals(mcType)) {
            map.put("mcType", "随机立减");
            entity.setMcMethod("0");
        } else if ("02".equals(mcType)) {
            map.put("mcType", "固定折扣立减");
            entity.setMcMethod("0");
        } else if ("03".equals(mcType)) {
            map.put("mcType", "随机折扣立减");
            entity.setMcMethod("0");
        } else if ("10".equals(mcType)) {
            map.put("mcType", "免费型代金券");
            entity.setMcMethod("1");
        } else if ("11".equals(mcType)) {
            map.put("mcType", "购买型代金券");
            entity.setMcMethod("1");
        } else {
            map.put("mcType", mcType);
        }
        entity.setMcType(mcType);
        entity.setCouponDateFlag(mcInfoBean.getCouponDateFlag());
        if (StringUtils.isNotBlank(mcInfoBean.getCouponStartDate())) {
            Date cstart = DateUtils.parseDate(mcInfoBean.getCouponStartDate(), DateUtils.FORMAT_YYYYMMDD);
            entity.setCouponStartDate(DateUtils.formatDate(cstart, DateUtils.DEFAULT_DATE_FORMAT));
        }
        if (StringUtils.isNotBlank(mcInfoBean.getCouponEndDate())) {
            Date cend = DateUtils.parseDate(mcInfoBean.getCouponEndDate(), DateUtils.FORMAT_YYYYMMDD);
            entity.setCouponEndDate(DateUtils.formatDate(cend, DateUtils.DEFAULT_DATE_FORMAT));
        }
        entity.setCouponPeriod(mcInfoBean.getCouponPeriod());
        entity.setCouponInventory(mcInfoBean.getCouponInventory());
        String availableDate = mcInfoBean.getAvailableDate();
        if ("0".equals(availableDate)) {
            map.put("availableDate", "立即生效");
        } else if ("1".equals(availableDate)) {
            map.put("availableDate", "第二天生效");
        } else {
            map.put("availableDate", availableDate);
        }
        entity.setAvailableDate(availableDate);
        Date start = DateUtils.parseDate(mcInfoBean.getMcStartDate(), DateUtils.FORMAT_YYYYMMDD);
        Date end = DateUtils.parseDate(mcInfoBean.getMcEndDate(), DateUtils.FORMAT_YYYYMMDD);
        entity.setMcStartDate(DateUtils.formatDate(start, DateUtils.DEFAULT_DATE_FORMAT));
        entity.setMcEndDate(DateUtils.formatDate(end, DateUtils.DEFAULT_DATE_FORMAT));
        entity.setMcTimeFlag(mcInfoBean.getMcTimeFlag());
        entity.setRestriction(mcInfoBean.getRestriction());
        String clientRange = mcInfoBean.getClientRange();
        if ("0".equals(clientRange)) {
            map.put("clientRange", "所有人");
        } else if ("1".equals(clientRange)) {
            map.put("clientRange", "指定用户");
        } else {
            map.put("clientRange", clientRange);
        }
        entity.setClientRange(clientRange);
        entity.setMerchantRange(mcInfoBean.getMerchantRange());
        if("1".equals(mcInfoBean.getMerchantRange())){
            entity.setNewMchntFlagDeposit(mcInfoBean.getNewMchntFlag());
            entity.setAmtLimitFloorDeposit(mcInfoBean.getAmtLimitFloor());
            entity.setAmtLimitCellingDeposit(mcInfoBean.getAmtLimitCelling());
        }
        if("2".equals(mcInfoBean.getMerchantRange())){
            entity.setNewMchntFlagMoney(mcInfoBean.getNewMchntFlag());
            entity.setAmtLimitFloorMoney(mcInfoBean.getAmtLimitFloor());
            entity.setAmtLimitCellingMoney(mcInfoBean.getAmtLimitCelling());
        }
        entity.setAuditStatus(mcInfoBean.getAuditStatus());
        //创建人、创建时间、活动状态
        entity.setCreateTime(mcInfoBean.getCreateTime());
        entity.setOprId(mcInfoBean.getOprId());
        entity.setMcStatus(mcInfoBean.getMcStatus());
        entity.setClaimChannelId(mcInfoBean.getClaimChannelId());
        map.put("entity", entity);

        //参与机构
        List<String> orgList = new ArrayList<>();
        List<McOrgInfoBean> orgInfoList = respBean.getOrgInfoList();
        if (!ObjectUtils.isEmpty(orgInfoList)) {
            for (McOrgInfoBean orgBean : orgInfoList) {
                orgList.add(orgBean.getOrgId());
            }
        }
        map.put("orgList", JSON.toJSONString(orgList));
        //指定用户手机号
        List<McUserInfoBean> userInfoPhones = new ArrayList<>();
        //指定用户银行卡卡号
        List<McUserInfoBean> userInfoCards = new ArrayList<>();
        //指定用户卡BIN
        List<McUserInfoBean> userInfoKbins = new ArrayList<>();
        //指定用户卡产品
        List<McUserInfoBean> userInfoKbinProds = new ArrayList<>();
        List<McUserInfoBean> userInfoList = respBean.getUserInfoList();
        if (!ObjectUtils.isEmpty(userInfoList)) {
            for (McUserInfoBean userBean : userInfoList) {
                if ("0".equals(userBean.getAcctType())) {
                    userInfoPhones.add(userBean);
                } else if ("1".equals(userBean.getAcctType())) {
                    userInfoCards.add(userBean);
                } else if ("2".equals(userBean.getAcctType())) {
                    userInfoKbins.add(userBean);
                } else if ("3".equals(userBean.getAcctType())) {
                    userInfoKbinProds.add(userBean);
                } else {
                    log.info("对客营销活动用户类型");
                }
            }
        }
        map.put("userInfoPhones", userInfoPhones);
        map.put("userInfoCards", userInfoCards);
        map.put("userInfoKbins", userInfoKbins);
        map.put("userInfoKbinProds", userInfoKbinProds);
        //指定商户
        List<McMchntInfoBean> merchantList = new ArrayList<>();
        //指定商圈
        List<McMchntInfoBean> circleList = new ArrayList<>();
        List<McMchntInfoBean> mchntInfoList = respBean.getMchntInfoList();
        if (!ObjectUtils.isEmpty(mchntInfoList)) {
            for (McMchntInfoBean infoBean : mchntInfoList) {
                if ("0".equals(infoBean.getMchntType())) {
                    merchantList.add(infoBean);
                } else if ("1".equals(infoBean.getMchntType())) {
                    circleList.add(infoBean);
                } else {
                    log.info("CustomerMarketService:setCouponMcModel");
                }
            }
        }
        map.put("mchntInfoOldList", JSON.toJSONString(mchntInfoList));
        map.put("merchantList", merchantList);
        map.put("circleList", circleList);
        //优惠策略
        //卡券类
        List<CustomerRuleInfoVo> ruleInfos = new ArrayList<>();
        List<CouponRuleInfoBean> ruleInfoList = respBean.getRuleInfoList();
        if (!ObjectUtils.isEmpty(ruleInfoList)) {
            for (CouponRuleInfoBean ruleBean : ruleInfoList) {
                CustomerRuleInfoVo customerRuleInfo = new CustomerRuleInfoVo();
                customerRuleInfo.setCriteria(ruleBean.getCriteria());
                customerRuleInfo.setPurchaseAmt(ruleBean.getPurchaseAmt());
                customerRuleInfo.setDenomination(ruleBean.getDenomination());
                customerRuleInfo.setQuantity(ruleBean.getQuantity());
                customerRuleInfo.setDayRedeemLimit(ruleBean.getDayRedeemLimit());
                customerRuleInfo.setPurchaseFlag(ruleBean.getPurchaseFlag());
                customerRuleInfo.setBatchTaskStatus(ruleBean.getBatchTaskStatus());
                ruleInfos.add(customerRuleInfo);
            }
        }
        map.put("ruleInfos", ruleInfos);
        //有效期内部分有效期的时段
        List<CustomerTimeInfoVO> timeInfos = new ArrayList<>();
        List<McTimeInfoBean> timeInfoList = respBean.getTimeInfoList();
        if (!ObjectUtils.isEmpty(timeInfoList)) {
            for (McTimeInfoBean timeBean : timeInfoList) {
                CustomerTimeInfoVO timeInfo = new CustomerTimeInfoVO();
                timeInfo.setStartTime(timeBean.getStartTime());
                timeInfo.setEndTime(timeBean.getEndTime());
                timeInfos.add(timeInfo);
            }
        }
        List<String> weekList = new ArrayList<>();
        if (!ObjectUtils.isEmpty((timeInfoList))) {
            McTimeInfoBean mcTimeInfoBean = timeInfoList.get(0);
            if ("1".equals(mcTimeInfoBean.getFlagMonday())) {
                weekList.add("1");
            }
            if ("1".equals(mcTimeInfoBean.getFlagTuesday())) {
                weekList.add("2");
            }
            if ("1".equals(mcTimeInfoBean.getFlagWednesday())) {
                weekList.add("3");
            }
            if ("1".equals(mcTimeInfoBean.getFlagThursday())) {
                weekList.add("4");
            }
            if ("1".equals(mcTimeInfoBean.getFlagFriday())) {
                weekList.add("5");
            }
            if ("1".equals(mcTimeInfoBean.getFlagSaturday())) {
                weekList.add("6");
            }
            if ("1".equals(mcTimeInfoBean.getFlagSunday())) {
                weekList.add("7");
            }
        }
        map.put("timeInfos", timeInfos);
        map.put("week", JSON.toJSONString(weekList));

        //线上、线下营销产品
        List<McProductInfoBean> productInfoList = respBean.getProductInfoList();
        List<String> lineProducekList = new ArrayList<>();
        if (!ObjectUtils.isEmpty((productInfoList))) {
            for (McProductInfoBean infoBean : productInfoList) {
                lineProducekList.add(infoBean.getBizProdCode());
            }
        }
        map.put("lineProducekList", JSON.toJSONString(lineProducekList));
        return respBean;
    }


    /**
     * 对客营销活动列表查询
     * 所有营销活动待审核列表查询
     *
     * @param queryRequest
     * @param reqDto
     * @return
     */
    public IPage<McListInfoBean> findMcByPage(QueryRequest queryRequest, QueryMcListInfoRequest reqDto) throws RedpException {
        Result<QueryMcListInfoResponse> res = new Result<>();
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        reqDto.setPageIndex(String.valueOf(queryRequest.getPageNum() - 1));
        reqDto.setPageSize(String.valueOf(queryRequest.getPageSize()));

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.QUERY_MC_LIST.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.QUERY_MC_LIST.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        try {
            log.info("查询营销活动列表 请求报文：{}", JSON.toJSONString(reqDto));
            res = queryMcListInfoService.queryMcList(reqDto);
            log.info("查询营销活动列表 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("营销活动列表查询时系统异常:[{}]", e);
            throw new RedpException("营销活动列表查询时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("营销活动列表查询时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("营销活动列表查询时接口调用失败");
        }
        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("营销活动列表查询时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("营销活动列表查询时失败:" + res.getResult().getRespDesc());
        }

        // 应答
        Page<McListInfoBean> page = new Page<>();
        List<McListInfoBean> list = Lists.newArrayList();
        if (Common.isNotBlank(res) && CollectionUtils.isNotEmpty(res.getResult().getMcInfoList())) {
            list = res.getResult().getMcInfoList();
            page.setRecords(list);
            page.setTotal(Integer.parseInt(res.getResult().getTotalRows()));
        } else {
            log.info("营销活动列表查询时没有返回数据:result=[{}]", JSON.toJSONString(res.getResult()));
            page.setRecords(list);
            page.setTotal(0);
        }
        return page;
    }

    /**
     * 减免类营销活动的管理，包括删除草稿、审核通过及退回
     *
     * @param entity
     */
    public void manageMoneyOffMc(MarketManagerRequest entity) throws RedpException {
        ManageMoneyOffMcRequest reqDto = getManagerMoneyOffMcBean(entity);
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());

        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.MONEYOFF_MC_MANAGER.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.MONEYOFF_MC_MANAGER.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        Result<ManageMoneyOffMcResponse> res = new Result<>();
        try {
            log.info(entity.getMsg() + " 请求报文：{}", JSON.toJSONString(reqDto));
            res = manageMcService.manageMoneyOffMc(reqDto);
            log.info(entity.getMsg() + " 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error(entity.getMsg() + "时系统异常:[{}]", e);
            throw new RedpException(entity.getMsg() + "时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error(entity.getMsg() + "时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException(entity.getMsg() + "时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error(entity.getMsg() + "时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException(entity.getMsg() + "时失败:" + res.getResult().getRespDesc());
        }
    }

    /**
     * 转换减免类营销管理时的请求对象
     *
     * @param entity
     * @return
     */
    private ManageMoneyOffMcRequest getManagerMoneyOffMcBean(MarketManagerRequest entity) {
        ManageMoneyOffMcRequest bean = new ManageMoneyOffMcRequest();
        bean.setMcNo(entity.getMcNo());
        bean.setOprType(entity.getOprType());
        bean.setOprContent(entity.getOprContent());
        bean.setQueryType(entity.getQueryType());
        bean.setQueryRole(entity.getQueryRole());
        return bean;
    }

    /**
     * 卡券类营销活动的管理，包括删除草稿、审核通过及退回
     *
     * @param entity
     */
    public void manageCouponMc(MarketManagerRequest entity) throws RedpException {
        ManageCouponMcRequest reqDto = getManagerCouponMcBean(entity);
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String orgCode = getOrgCode(user.getOrgCode());

        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));
        header.setTxnNum(MompTxnNumEnum.COUPON_MC_MANAGER.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.COUPON_MC_MANAGER.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        Result<ManageCouponMcResponse> res = new Result<>();
        try {
            log.info(entity.getMsg() + " 请求报文：{}", JSON.toJSONString(reqDto));
            res = manageMcService.manageCouponMc(reqDto);
            log.info(entity.getMsg() + " 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error(entity.getMsg() + "时系统异常:[{}]", e);
            throw new RedpException(entity.getMsg() + "时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error(entity.getMsg() + "时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException(entity.getMsg() + "时接口调用失败");
        }

        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error(entity.getMsg() + "时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException(entity.getMsg() + "时失败:" + res.getResult().getRespDesc());
        }
    }

    /**
     * 转换卡券类营销活动管理时的请求对象
     *
     * @param entity
     * @return
     */
    private ManageCouponMcRequest getManagerCouponMcBean(MarketManagerRequest entity) {
        ManageCouponMcRequest bean = new ManageCouponMcRequest();
        bean.setMcNo(entity.getMcNo());
        bean.setOprType(entity.getOprType());
        bean.setOprContent(entity.getOprContent());
        bean.setQueryType(entity.getQueryType());
        bean.setQueryRole(entity.getQueryRole());
        return bean;
    }

    /**
     * 营销打款
     *
     * @return
     */
    public OperationResult mompPayment(String mcNo, McBankAccountInfoBean accountInfoBean, UpmpUserInfoVO user, String adjustAmt) {
        MompPaymentRequest req = new MompPaymentRequest();
        req.setHeader(CommonUtils.packageBompHeader(bMchntBaseInfoService.genMerOrder(user.getInstId()), BompTxnNumEnum.mompPayment.getType(), user.getInstId(), null));
        req.setUserId(user.getId());
        req.setMchntNo(user.getOrgCode());
        req.setMcNo(mcNo);
        req.setPayerAcctNo(accountInfoBean.getPayAcctNo());
        req.setPayerAcctName(accountInfoBean.getPayAcctName());
        req.setCertNo(accountInfoBean.getIdNo());
        req.setCertType(accountInfoBean.getIdType());
        req.setMobile(accountInfoBean.getCellPhoneNumber());
        req.setAdjustAmt(adjustAmt);


        log.info(BompTxnNumEnum.mompPayment.getDesc() + "请求报文：" + JSON.toJSONString(req));

        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        try {
            cc.rengu.igas.bomp.facade.result.Result<MompPaymentResponse> response = bompManualAdjustAcctService.mompPayment(req);


            log.info(BompTxnNumEnum.mompPayment.getDesc() + "交易返回报文：" + JSON.toJSONString(response));

            if (response.isSuccess()) {
                MompPaymentResponse resp = response.getResult();
                // 返回码：01-记账成功 "ER12BM56","该营销活动已营销打款成功,本次交易拒绝" 其他均视为失败
                if (BusinessContainer.BSPS_SUCCESS.equals(resp.getRespCode()) || "ER12BM56".equals(resp.getRespCode())) {
                    if (IgasAcctStatusEnum.SUCCESS.getType().equals(resp.getAcctStatus())) {
                        result.put("respCode", resp.getRespCode());
                        result.put("respMsg", resp.getRespDesc());
                        log.info(BompTxnNumEnum.mompPayment.getDesc() + "记账状态正常");
                        return OperationResult.buildSuccessResult(BompTxnNumEnum.mompPayment.getDesc() + "交易成功", result);
                    } else {
                        result.put("respCode", BusinessContainer.ERROR);
                        result.put("respMsg", "记账状态异常");
                        log.error(BompTxnNumEnum.mompPayment.getDesc() + "记账状态异常");
                        return OperationResult.buildFailureResult(BompTxnNumEnum.mompPayment.getDesc() + "失败", result);
                    }
                } else {
                    result.put("respCode", resp.getRespCode());
                    result.put("respMsg", resp.getRespDesc());
                    return OperationResult.buildFailureResult(BompTxnNumEnum.mompPayment.getDesc() + "失败", result);
                }
            } else {
                result.put("resCode", BusinessContainer.ERROR);
                result.put("resMsg", BompTxnNumEnum.mompPayment.getDesc() + "没有获得返回报文");
                log.error(BompTxnNumEnum.mompPayment.getDesc() + "交易没有获得返回信息");
                return OperationResult.buildFailureResult(BompTxnNumEnum.mompPayment.getDesc() + "失败", result);
            }
        } catch (Exception e) {
            // TODO: handle exception
            result.put("resCode", BusinessContainer.ERROR);
            result.put("resMsg", e.getMessage());
            log.error(BompTxnNumEnum.mompPayment.getDesc() + "交易出现异常，异常信息：" + e.getMessage(), e);
            return OperationResult.buildFailureResult(BompTxnNumEnum.mompPayment.getDesc() + "失败", result);
        }
    }


    /**
     * 根据orgcode获取固定长度14位的orgCode
     * 超过14位截取前14位
     * 不够14位前面补0
     *
     * @param orgCode
     * @return
     */
    private String getOrgCode(String orgCode) {
        if (orgCode.length() >= 14) {
            return orgCode.substring(0, 14);
        }
        int length = orgCode.length();
        StringBuffer sb = null;
        while (length < 14) {
            sb = new StringBuffer();
            sb.append("0").append(orgCode);
            orgCode = sb.toString();
            length = orgCode.length();
        }
        return orgCode;
    }

    public Result<QueryMcMerUserListInfoResponse> getMchntResultList(QueryMcMerUserListRequest reqDto, String pageIndex, String pageSize, String queryType) throws RedpException {
        Result<QueryMcMerUserListInfoResponse> res = new Result<>();
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        String orgCode = getOrgCode(user.getOrgCode());
        //设置入参
        reqDto.setQueryId(user.getId());
        reqDto.setQueryOrgId(user.getOrgCode());
        reqDto.setPageIndex(pageIndex);
        reqDto.setPageSize(pageSize);
        //M--商户信息  U--用户信息   A--商圈信息
        reqDto.setTransType(queryType);

        //设置报文头
        Header header = new Header();
        header.setVersion(BusinessContainer.MOMP_VERSION);
        header.setTransDate(CommonUtils.getCurrentDate("yyyyMMdd"));
        header.setTransTime(CommonUtils.getCurrentDate("HHmmss"));

        header.setTxnNum(MompTxnNumEnum.MC_MER_USER_QUERY.getType());
        header.setBizType(BusinessContainer.MOMP_DEFAULT_BIZ_TYPE);
        header.setInstId(user.getInstId());
        header.setSrcSysId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setChanlId(BusinessContainer.MOMP_SRC_SYS_ID);
        header.setTraceNo(user.getInstId().substring(0, 3) + orgCode + MompTxnNumEnum.MC_MER_USER_QUERY.getType() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        reqDto.setHeader(header);

        try {
            log.info("查询营销活动商户列表 请求报文：{}", JSON.toJSONString(reqDto));
            res = detailInfoService.queryMcMerUserList(reqDto);
            log.info("查询营销活动商户列表 应答报文：{}", JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("查询营销活动商户列表查询时系统异常:[{}]", e);
            throw new RedpException("查询营销活动商户列表查询时系统异常,请联系管理员");
        }
        if (null == res || null == res.getResult()) {
            log.error("查询营销活动商户列表查询时接口调用失败:result=[{}]", JSON.toJSONString(res));
            throw new RedpException("查询营销活动商户列表查询时接口调用失败");
        }
        if (!BusinessContainer.BSPS_SUCCESS.equals(res.getResult().getRespCode())) {
            log.error("查询营销活动商户列表查询时失败:code=[{}],msg=[{}]", res.getResult().getRespCode(), res.getResult().getRespDesc());
            throw new RedpException("查询营销活动商户列表查询时失败:" + res.getResult().getRespDesc());
        }
        return res;
    }

    /**
     * 获取商户信息列表
     *
     * @param reqDto
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public IPage<McMchntInfoBean> getMchntList(QueryMcMerUserListRequest reqDto, String pageIndex, String pageSize) throws RedpException {
        Result<QueryMcMerUserListInfoResponse> res = getMchntResultList(reqDto, pageIndex, pageSize, BusinessContainer.MC_MER_USER_M);
        List<McMchntInfoBean> resultList = res.getResult().getMchntInfoList();
//        if (StringUtils.isNotBlank(mchntType)) {
//            resultList = resultList.stream().filter(n -> mchntType.equals(n.getMchntType())).collect(Collectors.toList());
//        }

        // 应答
        Page<McMchntInfoBean> page = new Page<>();
        if (Common.isNotBlank(resultList) && CollectionUtils.isNotEmpty(resultList)) {
            page.setRecords(resultList);
            page.setTotal(Integer.parseInt(res.getResult().getTotalRows()));
        } else {
            page.setRecords(new ArrayList<>());
            page.setTotal(0);
        }
        return page;
    }

    /**
     * 获取商圈信息
     *
     * @param mcNo
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<McMchntInfoBean> getMchntCircleList(String mcNo, String pageIndex, String pageSize) throws RedpException {

        QueryMcMerUserListRequest reqDto = new QueryMcMerUserListRequest();
        reqDto.setQueryType("0");
        reqDto.setQueryRole("00");
        reqDto.setMcNo(mcNo);

        Result<QueryMcMerUserListInfoResponse> result = getMchntResultList(reqDto, pageIndex, pageSize, BusinessContainer.MC_MER_USER_A);
        List<McMchntInfoBean> resultList = Lists.newArrayList();
        if (!ObjectUtils.isEmpty(result.getResult().getMchntInfoList())) {
            resultList = result.getResult().getMchntInfoList();
        }
        return resultList.stream().filter(n -> BusinessContainer.MC_MER_INFO_1.equals(n.getMchntType())).collect(Collectors.toList());
    }

}